/**
Company: Sarkar software technologys
WebSite: http://www.sarkar.cn
Author: yeganaaa
Date : 8/1/18
Time: 5:55 AM
 */

package com.ug_project.dict.engine.indextable

inline fun <T : Number> T.bit(bitIndex: Int): Boolean = ((1 shl bitIndex).toLong() and this.toLong()) > 0

inline fun <T : Number> T.bit(bitIndex: Int, value: Boolean): T {
    var long = this.toLong()

    long = if (value)
        long or (1 shl bitIndex).toLong() //long |= 1 << bitIndex
    else
        long and (1 shl bitIndex).inv().toLong() //long &= ~(1 << bitIndex)

    return long as T
}


inline fun Number.printBits(){
    val size = when (this){
        is Byte -> 1 * 8
        is Short -> 2 * 8
        is Int -> 4 * 8
        is Long -> 8 * 8
        else -> throw Exception("Unsupported, Plase register before use")
    }
    for (i in size - 1 downTo 0){
        print(if (this.bit(i)) "1" else "0")
    }
    println()
}